package TUILayer;

import ControlLayer.LeaseCtr;
import ModelLayer.RealException;


public class LeaseUI {
	
	private LeaseCtr leaseCtr;
	private LeaseItemMenuUI leaseItemMenuUI;
	private InputParser inputParser;
	private LoginInfo loginInfo; 
	
	public LeaseUI(InputParser inputParser, LoginInfo loginInfo)
	{
		leaseCtr = new LeaseCtr();
		leaseItemMenuUI = new LeaseItemMenuUI(inputParser);
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}
	
	public void start() throws RealException 
	{
		
		boolean exit = false;
		int choise = 0;
		switch(loginInfo.getLoggedEmployeePositionId())
		{
		case 1:
		{
			do {
				try {
					choise = inputParser.getInt("\f *** LeaseMenu ***\n\n1. Show a lease information\n2. Create lease\n3. Update lease\n4. Delete lease\n5. Return lease\n6. List all borrowed Lease\n7. List all returned lease\n8. Return\n\nMake your choise: ");
					switch (choise) {
					case 1:
						showInfo();
						break;
					case 2:
						createLease();
						break;
					case 3:
						updateLease();
						break;
					case 4:
						deleteLease();
						break;
					case 5:
						returnLease();
						break;
					case 6:
						listAllBorrowedLease();
						break;
					case 7:
						listAllReturnedLease();
						break;
					case 8:
						exit = true;
						break;
					}
				}
				catch (RealException e) {
				System.out.println("\n" + e.getError() + "Please try again");
				inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 2:
		{
			do {
				try {
					choise = inputParser.getInt("\f *** LeaseMenu ***\n\n1. Show a lease informationn2. List all borrowed Lease\n3. List all returned lease\n4. Return\n\nMake your choise: ");
					switch (choise) {
					case 1:
						showInfo();
						break;
					case 2:
						listAllBorrowedLease();
						break;
					case 3:
						listAllReturnedLease();
						break;
					case 4:
						exit = true;
						break;
					}
				}
				catch (RealException e) {
				System.out.println("\n" + e.getError() + "Please try again");
				inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 3:
		{
			do {
				try {
					choise = inputParser.getInt("\f *** LeaseMenu ***\n\n1. Show a lease information\n2. Create lease\n3. Update lease\n4. Delete lease\n5. Return lease\n6. List all borrowed Lease\n7. List all returned lease\n8. Return\n\nMake your choise: ");
					switch (choise) {
					case 1:
						showInfo();
						break;
					case 2:
						createLease();
						break;
					case 3:
						updateLease();
						break;
					case 4:
						deleteLease();
						break;
					case 5:
						returnLease();
						break;
					case 6:
						listAllBorrowedLease();
						break;
					case 7:
						listAllReturnedLease();
						break;
					case 8:
						exit = true;
						break;
					}
				}
				catch (RealException e) {
				System.out.println("\n" + e.getError() + "Please try again");
				inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		}
	}
	
	// show a sale information
	public void showInfo() throws RealException
	{
		int id = inputParser
				.getInt("\f ***ShowInfoMenu ***\n\nPlease input the id of the sale: ");
		System.out.println("\f ***ShowInfoMenu ***\n");
		System.out.println(leaseCtr.printLease(id));
		inputParser.printReturn();
	}
	
	public void createLease()
	{
		
		int customerId = inputParser
				.getInt("\f ***CreateLeaseMenu ***\n\nPlease input the id of the customer: ");
		int employeeId = loginInfo.getLoggedEmployeeId();
		long startDate =inputParser.getDate("\f ***CreateLeaseMenu ***\n\nPlease input the start date of the sale(dd/mm/yyyy): ");
		long endDate;
		do {
			long inputEndDate = inputParser.getDate("\f ***CreateLeaseMenu ***\n\nPlease input the end date of the sale(dd/mm/yyyy): ");
			endDate = inputEndDate;
			if (endDate < startDate) {
				System.out.println("\nPlease input correct end date");
				inputParser.printReturn();
			}
		} while(endDate < startDate);
		
		try{
			int leaseId = leaseCtr.createLease(customerId, employeeId, startDate, endDate);
			System.out.println("\nThe new lease id: " + leaseId);
			
			
			// Is the customer wants to pick up the products?
			int select = 0;
			
			select = inputParser
						.getInt("\f\n Would you like to pick up the product yourself?\n1. Yes\n2. No\nPlease make your choice: ");
			switch (select) {
				case 1:
					leaseCtr.hasPickup(leaseId);// customer can get pick up discount
					break;
				case 2:
					String address = inputParser.getString("\f\nPlease input the delivery address: ");
					leaseCtr.noPickup(leaseId, address);
					break;
				}
			
			// continue to leaseItemMenu
				leaseItemMenuUI.start(leaseId);
		}
		catch(RealException e)
		{
			System.out.println("\n" + e.getError() + "Please try again");
			inputParser.printReturn();
		}
	}
	
	public void updateLease() throws RealException 
	{
		int leaseId = inputParser
				.getInt("\f ***UpdateLeaseMenu ***\n\nPlease input the id of the lease: ");
		
		leaseCtr.getlease(leaseId);
		int choise = 0;
		String value = "";
		boolean exit = false;
		do {
			choise = inputParser
					.getInt("\f ***UpdateLeaseMenu ***\n\n1. Edit employee\n2. Edit customer\n3. Edit start date\n4. Edit end date\n5. Edit lease item units\n6. Return\n\nMake your choise: ");
			
			switch (choise) {
				case 1:
					value = String.valueOf(inputParser
						.getInt("\f ***UpdateLeaseMenu ***\n\nPlease input the new employee id: "));
					break;
				case 2:
					value = String.valueOf(inputParser
						.getInt("\f ***UpdateLeaseMenu ***\n\nPlease input the new customer id: "));
					break;
				case 3:
					value = String.valueOf(inputParser.getDate("\f ***UpdateLeaseMenu ***\n\nPlease input the new start date (dd/mm/yyyy): "));
					break;
				case 4:
					value = String.valueOf(inputParser.getDate("\f ***UpdateLeaseMenu ***\n\nPlease input the new end date (dd/mm/yyyy): "));
					break;
				case 5:
					leaseItemMenuUI.start(leaseId);
					break;
				case 6:
					exit = true;
					break;
				}
			
			leaseCtr.updateLease(leaseId, choise, value);
			inputParser.printReturn();
		} while (!exit);
	}
	
	public void deleteLease() throws RealException
	{
		int id = inputParser
				.getInt("\f ***DeleteLeaseMenu ***\n\nPlease input the id of the lease: ");
		leaseCtr.removeLease(id);
		System.out.println("\nlease with id " + id + " has been deleted");
		inputParser.printReturn();
	}
	
	public void returnLease() throws RealException
	{
		int id = inputParser
				.getInt("\f ***ReturnStatusMenu ***\n\nPlease input the id of the lease: ");
		String returnDate = String.valueOf(inputParser.getDate("\f ***ReturnLeaseMenu ***\n\nPlease input the return date (dd/mm/yyyy): "));
		leaseCtr.returnLease(id, returnDate);
		System.out.print("\nThe lease with id " + id + " is returned");
		inputParser.printReturn();
	}
	
	public void listAllBorrowedLease()
	{
		System.out.println("\f ***ListAllBorrowedLeaseMenu ***\n");
		System.out.println(leaseCtr.printAllBorrowedLease());
		inputParser.printReturn();
	}
	
	public void listAllReturnedLease()
	{
		System.out.println("\f ***ListAllReturnedLeaseMenu ***\n");
		System.out.println(leaseCtr.printAllReturnedLease());
		inputParser.printReturn();
	}
}
